package com.sctvcloud.linshui.ui.adapter.holder;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.linshui.R;
import com.sctvcloud.linshui.application.Cache;
import com.sctvcloud.linshui.ui.utils.UIUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

public class ChatRoomMsgHolder extends BaseRecylerHolder<Message> {
    @BindView(R.id.item_layout)
    protected RelativeLayout item_layout;
    @BindView(R.id.other_send_msg_layout)
    protected RelativeLayout other_send_msg_layout;
    @BindView(R.id.mine_send_msg_layout)
    protected RelativeLayout mine_send_msg_layout;

    @BindView(R.id.other_msg_avatar)
    protected ImageView other_msg_avatar;
    @BindView(R.id.other_msg_nick_name)
    protected CustomFontTextView other_msg_nick_name;
    @BindView(R.id.other_msg_content)
    protected CustomFontTextView other_msg_content;

    @BindView(R.id.mine_msg_avatar)
    protected ImageView mine_msg_avatar;
    @BindView(R.id.mine_msg_nick_name)
    protected CustomFontTextView mine_msg_nick_name;
    @BindView(R.id.mine_msg_content)
    protected CustomFontTextView mine_msg_content;

    private OnChatItemClickListener listener;

    public ChatRoomMsgHolder(Context context, View itemView, OnChatItemClickListener listener) {
        super(context, itemView);
        this.listener = listener;
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(final Message msg) {
        UserInfo myUserInfo = Cache.getInstance().getImUserInfo();
        item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onItemClick(msg);
                }
            }
        });
        if (myUserInfo.getUserID() == msg.getFromUser().getUserID()) {
            //当前用户发送
            other_send_msg_layout.setVisibility(View.GONE);
            mine_send_msg_layout.setVisibility(View.VISIBLE);
//            GlideUtil.getDynamicGlid(context,msg.getFromUser().getAvatar(),true).into(mine_msg_avatar);
            mine_msg_nick_name.setText(msg.getFromUser().getNickname());
            String s = msg.getContent().toJson();
            try {
                JSONObject object = new JSONObject(s);
                String text = object.getString("text");
                mine_msg_content.setText(text);
                //text是发送的内容
            } catch (JSONException e) {
                mine_msg_content.setText("");
                e.printStackTrace();
            }
//            if (!TextUtils.isEmpty(msg.getFromUser().getAvatar())) {
                msg.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (null != bitmap) {
                            mine_msg_avatar.setImageBitmap(UIUtils.toRoundBitmap(bitmap));
                        } else {
                            mine_msg_avatar.setImageResource(R.mipmap.icon_circle_avatar);
                        }
                    }
                });
//            }else {
//                other_msg_avatar.setImageResource(R.mipmap.icon_circle_avatar);
//            }
        } else {
            //其他用户发送
            other_send_msg_layout.setVisibility(View.VISIBLE);
            mine_send_msg_layout.setVisibility(View.GONE);
//            GlideUtil.getDynamicGlid(context,msg.getFromUser().getAvatar(),true).centerCrop().into(other_msg_avatar);
            other_msg_nick_name.setText(msg.getFromUser().getNickname());
            String s = msg.getContent().toJson();
            try {
                JSONObject object = new JSONObject(s);
                String text = object.getString("text");
                other_msg_content.setText(text);
                //text是发送的内容
            } catch (JSONException e) {
                other_msg_content.setText("");
                e.printStackTrace();
            }
//            if (!TextUtils.isEmpty(msg.getFromUser().getAvatar())) {
                msg.getFromUser().getAvatarBitmap(new GetAvatarBitmapCallback() {
                    @Override
                    public void gotResult(int i, String s, Bitmap bitmap) {
                        if (null != bitmap) {
                            other_msg_avatar.setImageBitmap(UIUtils.toRoundBitmap(bitmap));
                        } else {
                            other_msg_avatar.setImageResource(R.mipmap.icon_circle_avatar);
                        }
                    }
                });
//            }else {
//                other_msg_avatar.setImageResource(R.mipmap.icon_circle_avatar);
//            }
        }


    }

    public interface OnChatItemClickListener {
        void onItemClick(Message message);
    }
}
